<?php

class Model_System_Ftphelper {
	
	public function getNewFileList(){
		date_default_timezone_set("Australia/Melbourne");
		$imgFolder = getcwd().'/product_img/home_img/';
		$arrFileList = self::getDirectoryList($imgFolder);
		d($arrFileList);
		$intDayBegin = Model_DatetimeHelper::tranferToInt(Model_DatetimeHelper::dateToday()); 
		$arrRes = array();

		foreach($arrFileList as $file){
			
			$arrFile = explode('.',$file);

			
			if(strtolower(end($arrFile)) == 'jpg' ){
	
			$intTime = filemtime($imgFolder.$file);
				
				if($intTime >= $intDayBegin){
							
					$arrRes[] = $file;
				}
				
			}
		}
		return $arrRes;
		
	}

	public function getDirectoryList ($directory)
	{
	
		// create an array to hold directory list
		$results = array();
	
		// create a handler for the directory
		$handler = opendir($directory);
	
		// open directory and walk through the filenames
		while ($file = readdir($handler)) {
	
			// if file isn't this directory or its parent, add it to the results
			if ($file != "." && $file != "..") {
				
			
				$results[] = $file;
			
			}
	
		}
	
		// tidy up: close the handler
		closedir($handler);
	
		// done!
		return $results;
	
	}
	
	public function ftpUploadFile($fileName,$ftpServer,$userName,$passWord){
		
		$imgFolder = getcwd().'/product_img/home_img/';
		$remoteFolder = '/public_html/product_img/home_img/';
		
		$conn = ftp_connect($ftpServer);
		if($conn){
			$loginResult = ftp_login($conn, $userName, $passWord);
			if($loginResult){
				ftp_chdir($conn, $remoteFolder);
				$res = ftp_put($conn, $fileName, $imgFolder.$fileName, FTP_BINARY);
				return $res;
			}
			
			
		}
		
		return false;
	}
	
	public function uploadNg($fileName){
		
		return self::ftpUploadFile($fileName,'ftp.newgenerationgroup.com.au','newgener', '031788Office');
		
	}
	public function uploadLb($fileName){
		
		return self::ftpUploadFile($fileName,'ftp.lovebargain.com.au','lovebarg', 'Office051788');
		
	}
	public function uploadPc($fileName){

		return self::ftpUploadFile($fileName,'ftp.phonecollection.com.au','phonecol', 'Office051788');
		
	}
}

?>